package src;
import java.util.Comparator;

/**
 * The ExpressionComparator class, in which implements the Expression Interface
 * , compares between any two given expressions.
 *
 * @author Intro111 Staff
 */


public class ExpressionComparator implements Comparator {

    /**
     * Compares two expressions
     *
     * @param first  Object (must be an Expression)
     * @param second Object to compare to (must be an Expression)
     * @return int 0 if o1 and o2 are equal, 1 if o1 is larger than o2, -1 if o2 is larger than o1
     * @throws RunTimeException for non-expressions input
     */

    public int compare(Object first, Object second) {
        if (!((first instanceof Expression) && (second instanceof Expression))) {
            throw new RuntimeException("ExpressionComparator compare got a non-Expression argument.");
        }

        double firstValue = ((Expression) first).evaluate();
        double secondValue = ((Expression) second).evaluate();
        if (firstValue < secondValue)
            return -1;
        else if (firstValue > secondValue)
            return 1;
        else
            return 0;
    }

}
